/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckdiffMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.param.SQLParameter;
import com.yonyou.iuap.yms.processor.AutoBeanListProcessor;
import com.yonyou.iuap.yms.processor.AutoBeanProcessor;
import com.yonyou.iuap.yms.processor.ColumnProcessor;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.entity.Checkdiff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.List;

@Repository
public class CheckdiffMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public List<Checkdiff> selectCheckdiff(String tenant_id, List<String> orgid) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_checkdiff where ytenant_id=? and `dr`=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(orgid)) {
			sqlBuilder.append(" and warehouseId in ( ");
			String separator0 = " , ";
			for (int index = 0; index < orgid.size(); index++) {
				String item = orgid.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < orgid.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}/*else{
			sqlBuilder.append(" and warehouseId in ('')");
		}*/
		sqlBuilder.append(" and `dr`=0; ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new Checkdiff());
        return (List<Checkdiff>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public Checkdiff selectCheckDiffById(String tenant_id, String Id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_checkdiff where  ytenant_id=? and `dr`=0  and id= ? and `dr`=0;\n";
        parameter.addParam(tenant_id);
		parameter.addParam(Id);

        ResultSetProcessor processor = new AutoBeanProcessor(new Checkdiff());
        return (Checkdiff) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int UpdateCheckDiffListStatus(List<String> strList, String status, String tenant_id) throws Exception {
		if (strList == null || strList.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update dxq_checkdiff set status=?, modify_time=now() ");
		parameter.addParam(status);
		sqlBuilder.append(" where ytenant_id=? and `dr`=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and id in ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('') ");
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }

    public int insertCheckdiff(String id, String warehouseId, String checkResultID, String checkID, String coderfid, String localtionID, String product_id, String product_sku_id, String remark, String checkstatus, String status, String tenant_id, String pk_temp, String creator, String modifier) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "INSERT INTO`dxq_checkdiff`(`id`,`warehouseId`,`checkResultID`,`checkID`,`coderfid`,`localtionID`,`product_id`,`product_sku_id`,`remark`,`checkstatus`,`status`,`dr`,\n" + 
			         "`tenant_id`,`ytenant_id`,`pk_temp`,`version`,`create_time`,`modify_time`,`creator`,`modifier`)VALUES(?,?,?,?,?,?,?,?,?,?,0,?,?,?,0,now(),now(),?,?)                 \n";
        parameter.addParam(id);
		parameter.addParam(checkResultID);
		parameter.addParam(checkID);
		parameter.addParam(coderfid);
		parameter.addParam(localtionID);
		parameter.addParam(product_id);
		parameter.addParam(product_sku_id);
		parameter.addParam(remark);
		parameter.addParam(checkstatus);
		parameter.addParam(status);
		parameter.addParam(tenant_id);
		parameter.addParam(tenant_id);
		parameter.addParam(pk_temp);
		parameter.addParam(creator);
		parameter.addParam(modifier);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int selectCheckDiffCount(String checkCode, List<String> LocationArr, String productname, String status, String checkstatus, String productskuname, String tenant_id, List<String> warehouseIds) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" SELECT COUNT(c.`id`) AS numcount FROM dxq_checkdiff c where 1=1 ");
		if (CollectionUtil.isNotEmpty(LocationArr)) {
			sqlBuilder.append(" and c.localtionID in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < LocationArr.size(); index++) {
				String item = LocationArr.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < LocationArr.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}
		if (productname != null && !"".equals(productname)) {
			sqlBuilder.append(" and c.productName like CONCAT('%',?,'%') ");
			parameter.addParam(productname);
		}
		if (checkCode != null && !"".equals(checkCode)) {
			sqlBuilder.append(" and c.checkCode like CONCAT('%',?,'%') ");
			parameter.addParam(checkCode);
		}
		if (checkstatus != null && !"".equals(checkstatus)) {
			sqlBuilder.append(" and c.checkstatus=? ");
			parameter.addParam(checkstatus);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and c.status=? ");
			parameter.addParam(status);
		}
		sqlBuilder.append(" and c.dr=0 and c.ytenant_id=? ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(warehouseIds)) {
			sqlBuilder.append(" and c.warehouseId in ");
			sqlBuilder.append(" ( ");
			String separator1 = " , ";
			for (int index = 0; index < warehouseIds.size(); index++) {
				String item = warehouseIds.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < warehouseIds.size() - 1) {
					sqlBuilder.append(separator1);
				}
			}
			sqlBuilder.append(" ) ");
		}
		sqlBuilder.append(" ORDER BY c.`modify_time` DESC ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new ColumnProcessor(int.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public List<Checkdiff> selectCheckDiffPage(String checkCode, List<String> LocationArr, String productname, String status, String checkstatus, String productskuname, Integer startNumber, Integer pagesize, String tenant_id, List<String> warehouseIds) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" SELECT c.id,c.warehouseId,c.checkResultID,c.checkID,c.coderfid,c.localtionID,c.remark,c.checkstatus,c.status,c.RfidCode as RfidCode,c.LabelCount as LabelCount,c.productName,c.checkCode as checkCode FROM dxq_checkdiff c where 1=1 ");
		if (CollectionUtil.isNotEmpty(LocationArr)) {
			sqlBuilder.append(" and c.localtionID in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < LocationArr.size(); index++) {
				String item = LocationArr.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < LocationArr.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}
		if (productname != null && !"".equals(productname)) {
			sqlBuilder.append(" and c.productName like CONCAT('%',?,'%') ");
			parameter.addParam(productname);
		}
		if (checkCode != null && !"".equals(checkCode)) {
			sqlBuilder.append(" and c.checkCode like CONCAT('%',?,'%') ");
			parameter.addParam(checkCode);
		}
		if (checkstatus != null && !"".equals(checkstatus)) {
			sqlBuilder.append(" and c.checkstatus=? ");
			parameter.addParam(checkstatus);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and c.status=? ");
			parameter.addParam(status);
		}
		sqlBuilder.append(" and c.dr=0 and c.ytenant_id=? ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(warehouseIds)) {
			sqlBuilder.append(" and c.warehouseId in ");
			sqlBuilder.append(" ( ");
			String separator1 = " , ";
			for (int index = 0; index < warehouseIds.size(); index++) {
				String item = warehouseIds.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < warehouseIds.size() - 1) {
					sqlBuilder.append(separator1);
				}
			}
			sqlBuilder.append(" ) ");
		}
		sqlBuilder.append(" ORDER BY c.`modify_time` DESC LIMIT ?, ? ");
		parameter.addParam(startNumber);
		parameter.addParam(pagesize);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new Checkdiff());
        return (List<Checkdiff>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<Checkdiff> selectCheckdiffByIds(String tenant_id, List<String> ids) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select * from dxq_checkdiff where ytenant_id=? and `dr`=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(ids)) {
			sqlBuilder.append(" and id in ( ");
			String separator0 = " , ";
			for (int index = 0; index < ids.size(); index++) {
				String item = ids.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < ids.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}
		sqlBuilder.append(" and `dr`=0; ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new Checkdiff());
        return (List<Checkdiff>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

    public int UpdateCheckDiffStatus(String checkID, String status, String tenant_id) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_checkdiff set status=?, modify_time=now() where id=? and  ytenant_id=? and `dr`=0;\n";
        parameter.addParam(status);
		parameter.addParam(checkID);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }

	public int insertBatch(List<Checkdiff> insertBatch, String tenant_id) throws Exception {
		SQLParameter parameter = new SQLParameter(true);
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into dxq_checkdiff (`version`,`pk_temp`, `dr`,`tenant_id`,`ytenant_id`, " +
				"id, checkResultID, `checkID`, checkCode, coderfid, RfidCode, product_id, productName, product_sku_id, " +
				"warehouseId, localtionID, remark, checkstatus, LabelCount, status, creator, modifier, create_time, modify_time) values ");
		if (CollectionUtil.isNotEmpty(insertBatch)) {
			String separator0 = " , ";
			for (int i = 0; i < insertBatch.size(); i++) {
				Checkdiff item = insertBatch.get(i);
				sqlBuilder.append(" (0, '', 0, ?, ?, " +
						"?, ?, ?, ?, ?, ?, ?, ?, ?, " +
						"?, ?, ?, ?, ?, ?, ?, ?, now(), now()) ");
				parameter.addParamWithType(tenant_id, Types.VARCHAR);
				parameter.addParamWithType(tenant_id, Types.VARCHAR);

				parameter.addParamWithType(item.getId(), Types.VARCHAR);
				parameter.addParamWithType(item.getCheckResultID(), Types.VARCHAR);
				parameter.addParamWithType(item.getCheckID(), Types.VARCHAR);
				parameter.addParamWithType(item.getCheckCode(), Types.VARCHAR);
				parameter.addParamWithType(item.getCoderfid(), Types.VARCHAR);
				parameter.addParamWithType(item.getRfidCode(), Types.VARCHAR);
				parameter.addParamWithType(item.getProduct_id(), Types.VARCHAR);
				parameter.addParamWithType(item.getProductName(), Types.VARCHAR);
				parameter.addParamWithType(item.getProduct_sku_id(), Types.VARCHAR);

				parameter.addParamWithType(item.getWarehouseId(), Types.VARCHAR);
				parameter.addParamWithType(item.getLocaltionID(), Types.VARCHAR);
				parameter.addParamWithType(item.getRemark(), Types.VARCHAR);
				parameter.addParamWithType(item.getCheckstatus(), Types.VARCHAR);
				parameter.addParamWithType(item.getLabelCount(), Types.VARCHAR);
				parameter.addParamWithType(item.getStatus(), Types.VARCHAR);
				parameter.addParamWithType(item.getCreator(), Types.VARCHAR);
				parameter.addParamWithType(item.getModifier(), Types.VARCHAR);
				if (i < insertBatch.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}
		sqlBuilder.append(" ON DUPLICATE KEY UPDATE checkResultID=VALUES(checkResultID), checkID=VALUES(checkID), " +
				"checkCode=VALUES(checkCode), coderfid=VALUES(coderfid), RfidCode=VALUES(RfidCode), " +
				"product_id=VALUES(product_id), productName=VALUES(productName), product_sku_id=VALUES(product_sku_id), " +
				"warehouseId=VALUES(warehouseId), localtionID=VALUES(localtionID), remark=VALUES(remark), " +
				"checkstatus=VALUES(checkstatus), status=VALUES(status), LabelCount=VALUES(LabelCount), " +
				"modify_time=VALUES(modify_time), modifier=VALUES(modifier) ");


		String sql = sqlBuilder.toString();

		return ymsJdbcApi.update(sql, parameter);
	}
}
